// pages/environment/environment.js
const app = getApp(), core = require('../../utils/core');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    maxImage: 20,
    images: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOpenerEventChannel().on('infoTransfer', data => {
      this.setData({
        info: data.info,
      });
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.getOpenerEventChannel().emit('infoTransfer',{info:this.data.info});
  },
  //上传图
  uploadImages() {
    let page = this;
    let availableCount = page.data.maxImage - page.data.images.length;
    if(availableCount<1) {
      wx.showToast({title: '最多20张图片',icon:'none'});
      return;
    }
    wx.chooseImage({
      count: availableCount,
      success: res => {
        res.tempFiles.forEach(function(file){
          core.uploadFile({
            filePath: file.path,
            success(ret, response) {
              page.data.info.environment_images.push(ret.data.url);
              page.setData({info: page.data.info});
            }
          });
        });
      }
    });
  },
  bindRemoveImage(event) {
    let index = event.currentTarget.dataset.index;
    this.data.info.environment_images.splice(index,1);
    this.setData({'info.environment_images': this.data.info.environment_images});
  },
});
